package com.czy.basecommon.utils.tools;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.util.Log;
import java.math.BigInteger;

/**
 *
 * @author Jake Han
 */
public class NFCTool {
    
    private static final String TAG="NFCTool";

    private static String Right(String text, int length) {
        return text.substring(text.length() - length, text.length());
    }

    public static String getTAGID(Intent intent) {
        String action = intent.getAction();
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
            byte[] tagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
            String tID ="";
            int tagIDLength=10;
            String strTemp="0000000000";
            /* String tID = Right("00" + String.valueOf(Integer.toHexString(tagId[0])), 2)
                    + Right("00" + String.valueOf(Integer.toHexString(tagId[1])), 2)
                    + Right("00" + String.valueOf(Integer.toHexString(tagId[2])), 2)
                    + Right("00" + String.valueOf(Integer.toHexString(tagId[3])), 2);*/
            if(tagId.length>4 && tagId.length<=7){
            	tID = Right("00" + String.valueOf(Integer.toHexString(tagId[6])), 2);
            	tID += Right("00" + String.valueOf(Integer.toHexString(tagId[5])), 2);
            	tID += Right("00" + String.valueOf(Integer.toHexString(tagId[4])), 2);
            	tagIDLength=17;
            	strTemp="00000000000000000";
            }
            tID += Right("00" + String.valueOf(Integer.toHexString(tagId[3])), 2)
                    + Right("00" + String.valueOf(Integer.toHexString(tagId[2])), 2)
                    + Right("00" + String.valueOf(Integer.toHexString(tagId[1])), 2)
                    + Right("00" + String.valueOf(Integer.toHexString(tagId[0])), 2);
            BigInteger bi = new BigInteger(tID, 16);
            String pin =Right(strTemp + String.valueOf(bi), tagIDLength); //Right("0000000000" + String.valueOf(bi), 10);
            Log.i(TAG, "--TG�d��: " + pin);
            return pin;

        }
        return null;
    }


    /**
     * 转十六进制
     */
    public static String toHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = bytes.length - 1; i >= 0; --i) {
            int b = bytes[i] & 0xff;
            if (b < 0x10)
                sb.append('0');
            sb.append(Integer.toHexString(b));
            if (i > 0) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    /**
     * 转反向十六进制
     */
    public static String toReversedHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; ++i) {
            if (i > 0) {
                sb.append(" ");
            }
            int b = bytes[i] & 0xff;
            if (b < 0x10)
                sb.append('0');
            sb.append(Integer.toHexString(b));
        }
        return sb.toString();
    }

    /**
     * 转十进制
     */
    public static long toDec(byte[] bytes) {
        //Log.e(Constants.TAG,"数组长度"+bytes.length);
        long result = 0;
        long factor = 1;
        for (int i = 0; i < bytes.length; ++i) {
            long value = bytes[i] & 0xffl;
            result += value * factor;
            factor *= 256l;
         //   Log.e(Constants.TAG,"value="+value+",result="+result+",factor="+factor);
        }
        return result;
    }

    /**
     * 转反向十进制
     */
    public static long toReversedDec(byte[] bytes) {
        long result = 0;
        long factor = 1;
        for (int i = bytes.length - 1; i >= 0; --i) {
            long value = bytes[i] & 0xffl;
            result += value * factor;
            factor *= 256l;
        }
        return result;
    }

    public static String getTagId(Intent intent){
        Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);//也可使用intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)直接获取TAGID;
        byte[] tagbytes = tagFromIntent.getId();
        String NFC_ID = String.format("%d", toDec(tagbytes));
        return NFC_ID;
    }
}
